{% extends 'layout.html' %}
{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">表单</div>
            <div class="panel-body">

                <form id="formAdd">

                    <div class="clearfix">
                        {% for field in form %}
                            <div class="col-xs-6">
                                <div class="form-group" style="position: relative;margin-bottom: 20px;">
                                    <label>{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg" style="color: red;position:absolute;"></span>
                                    <span class="error-msg" style="color: red;position: absolute;"></span>
                                </div>
                            </div>
                        {% endfor %}
                        <div class="col-xs-12">
                            <button id="btnAdd" type="button" class="btn btn-primary">提 交</button>
                        </div>
                    </div>

                </form>
            </div>
                  <div class="panel-heading">
          <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
          任务列表
      </div>
      <!-- Table -->
      <table class="table table-bordered" >
        <thead>
          <tr>
            <th>ID</th>
            <th>标题</th>
            <th>级别</th>
            <th>负责人</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
        {% for obj in queryset %}
          <tr>
            <th>{{ obj.id }}</th>
              <td>{{ obj.title }}</td>
              <td>{{ obj.get_level_display }}</td>
              <td>{{ obj.user.username }}</td>
            <td>
                <a class="btn btn-danger btn-xs" href="/task/{{ obj.id }}/delete/">删除</a>
            </td>
          </tr>
        {% endfor %}
        </tbody>
      </table>
    </div>
       <ul class="pagination">
            {{ page_string }}
        </ul>
        </div>
        <hr/>
        <h1>Ajax学习</h1>

        <h3>示例1</h3>
        <input id="btn1" type="button" class="btn btn-primary" value="点击"/>
        
        <h3>示例2</h3>
        <input type="text" id="txtUser" placeholder="姓名"/>
        <input type="text" id="txtAge" placeholder="年龄"/>
        
        <input id="btn2" type="button" class="btn btn-primary" value="点击">
        <h3>示例3</h3>
        <form id="form3">
            <input type="text" name="txtUser" placeholder="姓名"/>
            <input type="text" name="txtAge" placeholder="年龄"/>
            <input type="text"  name="txtEmail" placeholder="邮箱"/>
            <input type="text" name="txtMore" placeholder="简介"/>
        </form>
        
        <input id="btn3" type="button" class="btn btn-primary" value="点击">
        
    </div>
{% endblock %}

{% block js %}
    <script type="text/javascript">
        $(function () {
            // 页面框架加载完成之后代码自动执行
            bindBtn1Event();
            bindBtn2Event();
            bindBtn3Event();
            bindBtnAddEvent();
            
        })

        function bindBtn1Event() {
            $("#btn1").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: "post",
                    data: {
                        n1: 123,
                        n2: 456
                    },
                    dataType : "JSON",
                    success: function (res) {
                        console.log(res);
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }
        function  bindBtn2Event() {
            $("#btn2").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: "post",
                    data: {
                        name: $("#txtUser").val(),
                        age: $("#txtAge").val()
                    },
                    dataType : "JSON",
                    success: function (res) {
                        console.log(res);
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }
        function  bindBtn3Event() {
            $("#btn3").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: "post",
                    data: $("#form3").serialize(),  // 如何输入的内容比较多的话，使用form，利用这种方法会直接打包内容，并发送到后台
                    dataType : "JSON",
                    success: function (res) {
                        console.log(res);
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }
        function  bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                // 每次该开始的时候将错误信息置为空
                $(".error-msg").empty();
                
                $.ajax({
                    url: '/task/add/',
                    type: "post",
                    data: $("#formAdd").serialize(),  // 如何输入的内容比较多的话，使用form，利用这种方法会直接打包内容，并发送到后台
                    dataType : "JSON",
                    success: function (res) {
                        if(res.status){
                            alert("添加成功");
                            // 使用js实现页面的刷新,因为ajaux是偷偷的发送对的请求，
                            location.reload();
                        }else{
                            $.each(res.error,function (name,data){
                                 // console.log(name,data);
                                $("#id_" + name).next().text(data[0]);
                            })
                        }
                    }
                })
            })
        }
        

    </script>
{% endblock %}
